#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint);  //去掉标题栏
    mainLayout = new QHBoxLayout(this);

    //左侧控制按钮页
    controlwidget = new controlWidget();
    Fun_updateControlWidget();
    connect(controlwidget,&controlWidget::signal_onClicked,this,&Widget::slot_changeWorkingState);
    connect(this,&Widget::signal_changeCurrentPercentage,controlwidget,&controlWidget::slot_onChangeCurrentPercentage);
    controlwidget->setScaledContents(true);
    controlwidget->setMaximumSize(100,100);

    //右侧信息展示页
    infoLayout = new QVBoxLayout();
    toolbarLayout = new QHBoxLayout();
    toolBarSpacer = new QSpacerItem(10,0,QSizePolicy::Expanding,QSizePolicy::Minimum);

    closeButton = new QToolButton();
    closeButton->setIcon(QIcon(":/new/prefix1/closeIcon.png"));
    connect(closeButton,&QPushButton::clicked,this,&Widget::slot_onClickedCloseButton);
    miniButton = new QToolButton();
    miniButton->setIcon(QIcon(":/new/prefix1/miniIcon.png"));
    connect(miniButton,&QPushButton::clicked,this,&Widget::slot_onClickedMiniButton);
    pinButton = new QToolButton();
    pinButton->setIcon(QIcon(":/new/prefix1/pin.png"));
    connect(pinButton,&QPushButton::clicked,this,&Widget::slot_onClickedPinButton);

    toolbarLayout->addItem(toolBarSpacer);
    toolbarLayout->addWidget(pinButton);
    toolbarLayout->addWidget(miniButton);
    toolbarLayout->addWidget(closeButton);

    timerLCD = new DigitalClock();
    timerLCD->setMinimumHeight(50);
//    timerLCD->setAutoFillBackground(true);
    timerLCD->setDecMode();  //十进制模式
    timerLCD->setDigitCount(8);  //设置显示位数
    timerLCD->setSegmentStyle(QLCDNumber::Flat);
    connect(timerLCD,&DigitalClock::signal_onClicked,this,&Widget::slot_onClickedLcdNumber);

    infoLabel = new QLabel("猪猪提醒旭旭离关机还有：");
    infoLayout->addLayout(toolbarLayout);
    infoLayout->addWidget(infoLabel);
    infoLayout->addWidget(timerLCD);
    mainLayout->addWidget(controlwidget);
    mainLayout->addLayout(infoLayout);
    Fun_updataLcdNumber();

    //加载样式文件
    QFile qssFile(":/new/prefix1/main.qss");
    qssFile.open(QFile::ReadOnly);
    if(qssFile.isOpen())
    {
        this->setStyleSheet(qssFile.readAll());
        qssFile.close();
    }
}

Widget::~Widget()
{

}

void Widget::Fun_startTimer()
{
    //获取用户输入的延迟时间。
    //启动定时器
    myTimer = new QTimer();
    myTimer->setInterval(1000);
    connect(myTimer,&QTimer::timeout,this,&Widget::slot_onTimeout);
    myTimer->start();
}

void Widget::Fun_stopTimer()
{
    //暂停计时器
    myTimer->stop();
    isWorking = false;
    Fun_updateControlWidget();
}

void Widget::slot_onTimeout()
{
    //更新计数值
    if(secValue)
        secValue--;
    else if(minValue){
        minValue--;
        secValue = 59;
    }
    else if(hourValue){
        hourValue--;
        secValue = 59;
        minValue = 59;
    }
    int tempTimeValueSum = (hourValue * 60 + minValue) *60 +secValue;
    double percentage = tempTimeValueSum / timeValueSum ;
    emit signal_changeCurrentPercentage(percentage);
    //更新LCDNumber的视图
    Fun_updataLcdNumber();
//    if(hourValue == 0 && minValue == 1 && secValue == 0)
//        QMessageBox::information(nullptr,"倒计时提示","离关机还有一分钟！注意保存当前的工作内容！",QMessageBox::Ok);
    if(!secValue&&!minValue&&!hourValue)
    {
        //执行关机指令
        Fun_shutdown();
        return ;
    }
}

void Widget::slot_changeWorkingState()
{
    if(isWorking)
    {
        isWorking = false;
        Fun_stopTimer();
    }
    else
    {
        isWorking = true;
        Fun_startTimer();
    }
    Fun_updateControlWidget();
}

void Widget::slot_onClickedLcdNumber()
{
    if(isWorking)
    {
        //暂停计时
        Fun_stopTimer();
    }
    //跳转到数据设置页面
    editeWidget = new EditWidget();
    connect(editeWidget,&EditWidget::signal_changeTime,this,&Widget::slot_changeTimeValue);
    editeWidget->show();

}

void Widget::slot_changeTimeValue(int tempHour,int tempMin, int tempSec)
{
    hourValue = tempHour;
    minValue = tempMin;
    secValue = tempSec;
    //用来计算百分比
    timeValueSum = ( tempHour * 60 + tempMin ) * 60 + tempSec;
    Fun_updataLcdNumber();
    //设置完成后，回到主界面自动开始计时
    slot_changeWorkingState();
}

void Widget::Fun_shutdown()
{
    //没有使用cmd指令来控制时间，而是用定时器来控制时间
    QString cmd = QString("shutdown -s -t 0");
    pro.start(cmd);
    pro.waitForStarted();
    pro.waitForFinished();
}

void Widget::Fun_cancelShutdown()
{
    QString cmd = QString("shutdown -a");
    pro.start(cmd);
    pro.waitForStarted();
    pro.waitForFinished();
}

void Widget::Fun_updataLcdNumber()
{
    timerLCD->display(QString("%1:%2:%3")
                      .arg(hourValue,2,10,QLatin1Char('0'))
                      .arg(minValue,2,10,QLatin1Char('0'))
                      .arg(secValue,2,10,QLatin1Char('0')));
}

void Widget::Fun_updateControlWidget()
{
    QImage *img = new QImage;
    if(isWorking)
        img->load(":/new/prefix1/stopIcon.png");
    else
        img->load(":/new/prefix1/startIcon.png");
    controlwidget->setPixmap(QPixmap::fromImage(*img));
}

//关闭按钮
void Widget::slot_onClickedCloseButton()
{
    this->close();
}

//Pin按钮
void Widget::slot_onClickedPinButton()
{
    Qt::WindowFlags flags = this->windowFlags();
    if(isPin)
    {
        flags = flags & (~Qt::WindowStaysOnTopHint);
        pinButton->setIcon(QIcon(":/new/prefix1/pin.png"));
    }
    else
    {
        pinButton->setIcon(QIcon(":/new/prefix1/pined.png"));
        flags = flags | Qt::WindowStaysOnTopHint;
    }
    isPin = !isPin;

    this->setWindowFlags(flags);
    this->show();
}

//最小化按钮
void Widget::slot_onClickedMiniButton()
{
    this->showMinimized();
}

void Widget::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //求坐标差值
        //当前点击坐标-窗口左上角坐标
        p = e->globalPos() - this->frameGeometry().topLeft();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        //移到左上角
        move(e->globalPos() - p);
    }
}
